import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples_vanilla/localization.dart';
import 'package:flutter_architecture_samples_vanilla/widgets/extra_actions_button.dart';
import 'package:flutter_architecture_samples_vanilla/widgets/filter_button.dart';
import 'package:flutter_architecture_samples_vanilla/widgets/stats_count.dart';
import 'package:flutter_architecture_samples_vanilla/widgets/typedefs.dart';
import 'package:todos_app_core/todos_app_core.dart';

import '../models.dart';
import '../widgets/todo_list.dart';

class HomeScreen extends StatefulWidget {
  final AppState appState;
  final TodoAdder addTodo;
  final TodoUpdater updateTodo;
  final TodoRemover removeTodo;
  final Function toggleAll;
  final Function clearCompleted;
  final Function inverseCompleted;
  const HomeScreen(
      {Key? key,
      required this.updateTodo,
      required this.appState,
      required this.addTodo,
      required this.removeTodo,
      required this.toggleAll,
      required this.clearCompleted,
      required this.inverseCompleted})
      : super(key: ArchSampleKeys.homeScreen);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  AppTab activeTab = AppTab.todos;
  VisibilityFilter activeFilter = VisibilityFilter.all;
  void _updateVisibility(VisibilityFilter filter) {
    setState(() {
      activeFilter = filter;
    });
  }

  void _updateTab(AppTab tab) {
    setState(() {
      activeTab = tab;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(VanillaLocalizations.of(context)!.appTitle),
        actions: [
          FilterButton(
            onSelected: _updateVisibility,
            activeFilter: activeFilter,
            isActive: activeTab == AppTab.todos,
          ),
          ExtraActionsButton(
            onSelected: (action) {
              if (action == ExtraAction.toggleAllComplete) {
                widget.toggleAll();
              } else if (action == ExtraAction.clearCompleted) {
                widget.clearCompleted();
              } else if (action == ExtraAction.inverseCompleted) {
                widget.inverseCompleted();
              }
            },
            allComplete: widget.appState.allComplete,
            hasCompletedTodos: widget.appState.hasCompletedTodos,
          )
        ],
      ),
      body: activeTab == AppTab.todos
          ? TodoList(
              updateTodo: widget.updateTodo,
              addTodo: widget.addTodo,
              removeTodo: widget.removeTodo,
              loading: widget.appState.isLoading,
              filteredTodos: widget.appState.filterTodos(activeFilter),
            )
          : StatsCounter(
              numActive: widget.appState.numActive,
              numCompleted: widget.appState.numCompleted,
            ),
      floatingActionButton: FloatingActionButton(
        key: ArchSampleKeys.addTodoFab,
        onPressed: () {
          Navigator.pushNamed(context, ArchSampleRoutes.addTodo);
        },
        child: Icon(Icons.add),
      ),
      bottomNavigationBar: BottomNavigationBar(
        key: ArchSampleKeys.tabs,
        currentIndex: AppTab.values.indexOf(activeTab),
        onTap: (index) {
          _updateTab(AppTab.values[index]);
        },
        items: AppTab.values.map((tab) {
          return BottomNavigationBarItem(
            icon: Icon(tab == AppTab.todos ? Icons.list : Icons.show_chart),
            label: tab == AppTab.stats
                ? ArchSampleLocalizations.of(context)!.stats
                : ArchSampleLocalizations.of(context)!.todos,
          );
        }).toList(),
      ),
    );
  }
}
